"""
Submission Controller
Handles business logic related to form submissions with Redis caching
"""

from app import RedisManager
from app.models.submission import Submission
from app.models.user import User
from app import db
from datetime import datetime

class SubmissionController:
    """Controller for submission-related operations with Redis caching"""
    
    @staticmethod
    def get_submission_with_cache(submission_id):
        """
        Retrieve submission details with Redis caching
        """
        # Try to get from cache first
        cached_submission = RedisManager.get_cached_submission(submission_id)
        if cached_submission:
            return cached_submission
        
        # If not in cache, get from database
        submission = Submission.query.get(submission_id)
        if submission:
            submission_data = {
                'id': submission.id,
                'user_id': submission.user_id,
                'form_id': submission.form_id,
                'answer_data': submission.answer_data,
                'previous_data': submission.previous_data,
                'version': submission.version,
                'submitted_at': submission.submitted_at.isoformat() if submission.submitted_at else None
            }
            
            # Cache the submission data
            RedisManager.cache_submission(submission_id, submission_data)
            return submission_data
        
        return None
    
    @staticmethod
    def add_recent_submission(user_id, submission_data):
        """
        Add a submission to user's recent submissions in Redis
        """
        RedisManager.add_user_recent_submission(user_id, submission_data)
    
    @staticmethod
    def get_user_recent_submissions(user_id, count=10):
        """
        Get user's recent submissions from Redis
        """
        return RedisManager.get_user_recent_submissions(user_id, count)
    
    @staticmethod
    def submit_form(user_id, form_id, answer_data, previous_data=None):
        """
        Submit a form and update related caches
        """
        # Get the next version number
        latest_submission = Submission.query.filter_by(
            user_id=user_id, 
            form_id=form_id
        ).order_by(Submission.version.desc()).first()
        
        version = 1
        if latest_submission:
            version = latest_submission.version + 1
        
        # Create the submission
        submission = Submission(
            user_id=user_id,
            form_id=form_id,
            answer_data=answer_data,
            previous_data=previous_data,
            version=version
        )
        
        db.session.add(submission)
        db.session.commit()
        
        # Prepare submission data for caching
        submission_data = {
            'id': submission.id,
            'user_id': submission.user_id,
            'form_id': submission.form_id,
            'answer_data': submission.answer_data,
            'previous_data': submission.previous_data,
            'version': submission.version,
            'submitted_at': submission.submitted_at.isoformat() if submission.submitted_at else None
        }
        
        # Update caches
        RedisManager.cache_submission(submission.id, submission_data)
        RedisManager.add_user_recent_submission(user_id, submission_data)
        RedisManager.increment_form_submissions_count(form_id)
        
        return submission